home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / BinaryOutputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  4.9 KB  |  204 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.Vector;
  7. import symjava.sql.SQLException;
  8.  
  9. public final class BinaryOutputStream extends OutputStream {
  10.    private final int METHOD_getData;
  11.    private final int METHOD_setData = 1;
  12.    private final int METHOD_rewind = 2;
  13.    private final int METHOD_setNull = 3;
  14.    private RemoteObject proxy;
  15.    private byte[] _data;
  16.    private int offset;
  17.    private boolean bClosed;
  18.    private final int MAX_BUF_SIZE = 4000;
  19.  
  20.    public BinaryOutputStream(int id, ClientSession session) throws SQLException {
  21.       this.proxy = new RemoteObject("ProxyStream", id, session);
  22.       this._data = new byte[4000];
  23.       this.offset = 0;
  24.       this.bClosed = false;
  25.       this.proxy.invokeMethod(2);
  26.    }
  27.  
  28.    public void sendData(InputStream in, int len) throws SQLException {
  29.       if (this.bClosed) {
  30.          throw new SQLException("Stream is closed.");
  31.       } else {
  32.          try {
  33.             this.flush();
  34.  
  35.             while(len > 4000) {
  36.                in.read(this._data, 0, 4000);
  37.                this.offset = 4000;
  38.                this.flush();
  39.                len -= 4000;
  40.             }
  41.  
  42.             in.read(this._data, 0, len);
  43.             this.offset = len;
  44.             this.flush();
  45.          } catch (IOException e) {
  46.             throw new SQLException(((Throwable)e).getMessage());
  47.          }
  48.       }
  49.    }
  50.  
  51.    public void close() throws IOException {
  52.       this.bClosed = true;
  53.    }
  54.  
  55.    public synchronized void write(int b) throws IOException {
  56.       if (this.bClosed) {
  57.          throw new IOException("Stream is closed.");
  58.       } else {
  59.          if (this.offset == this._data.length) {
  60.             this.flush();
  61.          }
  62.  
  63.          this._data[this.offset++] = (byte)b;
  64.       }
  65.    }
  66.  
  67.    public synchronized void write(byte[] b, int off, int len) throws IOException {
  68.       if (this.bClosed) {
  69.          throw new IOException("Stream is closed.");
  70.       } else {
  71.          int avail = this._data.length - this.offset;
  72.          if (len <= avail) {
  73.             System.arraycopy(b, off, this._data, this.offset, len);
  74.             this.offset += len;
  75.          } else {
  76.             this.flush();
  77.             byte[] temp = new byte[len];
  78.             System.arraycopy(temp, 0, b, off, len);
  79.             this.setData(temp, len);
  80.          }
  81.       }
  82.    }
  83.  
  84.    public synchronized void flush() throws IOException {
  85.       this.setData(this._data, this.offset);
  86.       this.offset = 0;
  87.    }
  88.  
  89.    void setData(byte[] b, int len) throws IOException {
  90.       if (len != 0) {
  91.          Vector params = new Vector();
  92.          params.addElement(new Param(0, len));
  93.          params.addElement(new Param(0, b, len));
  94.  
  95.          try {
  96.             this.proxy.invokeMethod(1, params);
  97.          } catch (Exception e) {
  98.             throw new IOException(((Throwable)e).getMessage());
  99.          }
  100.       }
  101.    }
  102.  
  103.    public void setNull() throws SQLException {
  104.       this.proxy.invokeMethod(3);
  105.    }
  106.  
  107.    public void setBytes(byte[] data) throws SQLException {
  108.       try {
  109.          this.flush();
  110.          this.setData(data, data.length);
  111.       } catch (IOException e) {
  112.          throw new SQLException(((Throwable)e).getMessage());
  113.       }
  114.    }
  115.  
  116.    public void setByte(byte data) throws SQLException {
  117.       try {
  118.          this.flush();
  119.          byte[] b = new byte[1];
  120.          b[0] = data;
  121.          this.setData(b, 1);
  122.       } catch (IOException e) {
  123.          throw new SQLException(((Throwable)e).getMessage());
  124.       }
  125.    }
  126.  
  127.    public void setShort(short data) throws SQLException {
  128.       try {
  129.          this.flush();
  130.          NetData d = new NetData(data);
  131.          byte[] b = d.getBytes();
  132.          this.setData(b, b.length);
  133.       } catch (IOException e) {
  134.          throw new SQLException(((Throwable)e).getMessage());
  135.       }
  136.    }
  137.  
  138.    public void setBoolean(boolean data) throws SQLException {
  139.       try {
  140.          this.flush();
  141.          NetData d = new NetData(data);
  142.          byte[] b = d.getBytes();
  143.          this.setData(b, b.length);
  144.       } catch (IOException e) {
  145.          throw new SQLException(((Throwable)e).getMessage());
  146.       }
  147.    }
  148.  
  149.    public void setInt(int data) throws SQLException {
  150.       try {
  151.          this.flush();
  152.          NetData d = new NetData(data);
  153.          byte[] b = d.getBytes();
  154.          this.setData(b, b.length);
  155.       } catch (IOException e) {
  156.          throw new SQLException(((Throwable)e).getMessage());
  157.       }
  158.    }
  159.  
  160.    public void setLong(long data) throws SQLException {
  161.       try {
  162.          this.flush();
  163.          NetData d = new NetData(data);
  164.          byte[] b = d.getBytes();
  165.          this.setData(b, b.length);
  166.       } catch (IOException e) {
  167.          throw new SQLException(((Throwable)e).getMessage());
  168.       }
  169.    }
  170.  
  171.    public void setFloat(float data) throws SQLException {
  172.       try {
  173.          this.flush();
  174.          NetData d = new NetData(data);
  175.          byte[] b = d.getBytes();
  176.          this.setData(b, b.length);
  177.       } catch (IOException e) {
  178.          throw new SQLException(((Throwable)e).getMessage());
  179.       }
  180.    }
  181.  
  182.    public void setDouble(double data) throws SQLException {
  183.       try {
  184.          this.flush();
  185.          NetData d = new NetData(data);
  186.          byte[] b = d.getBytes();
  187.          this.setData(b, b.length);
  188.       } catch (IOException e) {
  189.          throw new SQLException(((Throwable)e).getMessage());
  190.       }
  191.    }
  192.  
  193.    public void setString(String data) throws SQLException {
  194.       try {
  195.          this.flush();
  196.          NetData d = new NetData(data);
  197.          byte[] b = d.getBytes();
  198.          this.setData(b, b.length);
  199.       } catch (IOException e) {
  200.          throw new SQLException(((Throwable)e).getMessage());
  201.       }
  202.    }
  203. }
  204.